import React from 'react';
import {createForm} from 'rc-form';
import {Button, WhiteSpace,Modal,Toast} from 'antd-mobile';
import { hashHistory} from 'react-router';
import {List} from 'antd-mobile';
import request from '../../utils/request';
import 'antd-mobile/dist/antd-mobile.css';

const Item = List.Item;
const Brief = Item.Brief;
const alert = Modal.alert;
let userMsg = {};
class Usersetting extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            pickerValue: [],
            dpValue: null,
            distributor:false,
            supplier:false,
            distributorStatus:'',
            supplierStatus:'',
            imageUrl:''

        };
    }
    componentDidMount() {
        this.props.changeTitle('账户设置');
    }

    componentWillMount(){
        userMsg = this.props.location.state;
        this.setState({
            imageUrl:userMsg.user_picture === undefined ? 'http://192.168.0.131:8091/ecommerce/20171115103252.png' : userMsg.user_picture
        });
        request('UserApplyService','queryTaskByUserId', {
            method: 'POST',
            body: {
                userId: userMsg.user_id
            }
        }).then((value) => {
            if (value.data.result === 0){
                const { taskList } = value.data.resultData;
                if (taskList.length === 0){

                }else {
                    taskList.forEach((i) => {
                        if (i.task_type === 0){//分销商申请
                            if (i.status === 0){
                                this.setState({
                                    distributorStatus:'待审核'
                                })
                            }else if (i.status === 1){
                                this.setState({
                                    distributorStatus:'审核通过'
                                })
                            }else if (i.status === 2){
                                this.setState({
                                    distributorStatus:'审核不通过'
                                })
                            }
                        }else if (i.task_type === 1 ){//供应商申请
                            if (i.status === 0){
                                this.setState({
                                    supplierStatus:'待审核'
                                })
                            }else if (i.status === 1){
                                this.setState({
                                    supplierStatus:'审核通过'
                                })
                            }else if (i.status === 2){
                                this.setState({
                                    supplierStatus:'审核不通过'
                                })
                            }
                        }

                    });
                }
            }else if (value.data.result === 1){
                Toast.info("申请失败，请稍后再试");
            }
        })

    }

    //查看会员信息
    userDetail = (e) => {
        hashHistory.push({
            pathname: '/user/details',
            state: userMsg
        })
    }
    //退出登录
    loginOut = (e) => {
        localStorage.setItem("isLogin", 0);
        window.location.href = "./#/myMenu";
    }
    //账户安全
    security = (e) => {
        hashHistory.push({
            pathname: '/user/security',
            state: userMsg
        })
    }
    //分销商申请
    distributorApply = (e) => {
        const alertInstance = alert('分销商申请', '确定要申请分销商', [
            { text: '取消', onPress: () => {
                return;
            } ,style: 'default'
            },
            { text: '确定', onPress: () => {
                //将申请任务记录到会员申请成为分销商与供应商请求表中
                request('UserApplyService','addTask', {
                    method: 'POST',
                    body: {
                        userId: userMsg.user_id,
                        taskType: 0
                    }
                }).then(function (value) {
                    if (value.data.result === 0){
                        Toast.info("申请成功，待后台审核！",1);
                    }else if (value.data.result === 1){
                        Toast.info("申请失败，请稍后再试！",1);
                    }
                });
            }
            },
        ]);
        setTimeout(() => {
            // 可以调用close方法以在外部close
            alertInstance.close();
        }, 500000);
    }
    //供应商申请
    supplierApply = (e) => {
        hashHistory.push({
            pathname: '/user/supplierApply',
            state: userMsg
        })
    }

    render() {
        //获取用户信息
        const userName = userMsg.user_name;
        // const userId = userMsg.user_id;

        return (
            <div>
                <WhiteSpace/>
                <List className="my-list">
                    <Item align="top" thumb={this.state.imageUrl} multipleLine onClick={this.userDetail.bind(this)}>
                        {userName} <Brief>subtitle</Brief>
                    </Item>
                </List>
                <List className="my-list">
                    <Item onClick={this.security}>账户安全</Item>
                    <Item onClick={this.distributorApply}>分销商申请<span style={{color:'grey',fontSize:26}}>&nbsp;&nbsp;&nbsp;{this.state.distributorStatus}</span></Item>
                    <Item onClick={this.supplierApply}>供应商申请<span style={{color:'grey',fontSize:26}}>&nbsp;&nbsp;&nbsp;{this.state.supplierStatus}</span></Item>
                </List>
                <WhiteSpace/>
                <Button type="warning" size="large" onClick={this.loginOut}>退出登录</Button><WhiteSpace/>
            </div>
        );
    }
}

export default createForm()(Usersetting);
